package com.aube.base.card;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aube.base.model.OptionItem;
import com.aube.base.roundedimageview.RoundedImageView;
import com.aube.base.util.MagicTextView;
import com.aube.interact.base.R;
import com.huyn.baseframework.ImageLoader;
import com.huyn.baseframework.utils.Utils;

import static android.widget.LinearLayout.VERTICAL;

/**
 * Created by hill on 16/12/22.
 */

public class QuizOptionViewEx extends FrameLayout {
    private final String TAG = "Aube/QuizOptionViewEx";
    private Context mContext;
    private OptionViewCallback mCallback;

    private int mImageWidth, mImageHeight;

    public LinearLayout mLinearLy;
    public RoundedImageView mOptionImg;
    public TextView mOptionText;
    public TextView mPercentText;


    public QuizOptionViewEx(Context context) {
        this(context, null);
    }
    public QuizOptionViewEx(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public QuizOptionViewEx(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
    }

    public void initView(OptionItem data) {
        mImageWidth = mContext.getResources().getDimensionPixelOffset(R.dimen.optionimg_width);
        mImageHeight = mContext.getResources().getDimensionPixelOffset(R.dimen.optionimg_height);

        mLinearLy = new LinearLayout(mContext);
        mLinearLy.setOrientation(VERTICAL);

        mOptionImg = new RoundedImageView(mContext);
        mOptionImg.setScaleType(ImageView.ScaleType.CENTER_CROP);
        mOptionImg.setBackgroundColor(getResources().getColor(R.color.grey));
        int padding = mContext.getResources().getDimensionPixelOffset(R.dimen.optionimg_padding);
        mOptionImg.setPadding(padding, padding, padding, padding);
        mOptionImg.setCornerRadius(mContext.getResources().getDimensionPixelOffset(R.dimen.optionimg_radius));
        if(data.hasImg())
            ImageLoader.getInstance(getContext()).fillImage(mOptionImg, data.getImgUrl(), mImageWidth, mImageHeight);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(mImageWidth, mImageHeight);
        mLinearLy.addView(mOptionImg, params);

        mOptionText = new TextView(mContext);
        mOptionText.setText(data.optionValue);
        mOptionText.setTextColor(getResources().getColor(R.color.white));
        mOptionText.setTextSize(12);
        mOptionText.setMaxWidth(mImageWidth);
        mOptionText.setMaxLines(1);
        mOptionText.setGravity(Gravity.CENTER);
        mOptionText.setEllipsize(TextUtils.TruncateAt.END);
        params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, mContext.getResources().getDimensionPixelOffset(R.dimen.optionname_height));
        params.topMargin = Utils.dip2px(mContext, 5);
        params.gravity = Gravity.CENTER;
        mLinearLy.addView(mOptionText, params);

        mPercentText = new TextView(mContext);
        mPercentText.setTextColor(Color.WHITE);
        mPercentText.setTextSize(13);
        mPercentText.setGravity(Gravity.CENTER_VERTICAL);
        mPercentText.setBackground(getResources().getDrawable(R.drawable.gradient_bg_bottom));

        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        addView(mLinearLy, layoutParams);
        layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, Utils.dip2px(mContext, 20));
        layoutParams.topMargin = mContext.getResources().getDimensionPixelOffset(R.dimen.optionimg_percent_topmargin);
        layoutParams.gravity = Gravity.CENTER_HORIZONTAL;
        addView(mPercentText, layoutParams);
    }

    public void setLayoutParams(int index) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        if(index != 0) {
            params.leftMargin = Utils.dip2px(mContext, 10);
        }
        setLayoutParams(params);
    }
}
